package com.mune.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.mune.gulimall.product.entity.AttrEntity;
import com.mune.gulimall.product.service.AttrAttrgroupRelationService;
import com.mune.gulimall.product.service.AttrService;
import com.mune.gulimall.product.service.CategoryService;
import com.mune.gulimall.product.vo.AttrGroupRelationVo;
import com.mune.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.mune.gulimall.product.entity.AttrGroupEntity;
import com.mune.gulimall.product.service.AttrGroupService;
import com.mune.common.utils.PageUtils;
import com.mune.common.utils.R;


/**
 * 属性分组
 *
 * @author mune
 * @email 1356008591@qq.com
 * @date 2020-07-28 20:48:31
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
  @Autowired
  private AttrGroupService attrGroupService;
  @Autowired
  private CategoryService categoryService;
  @Autowired
  private AttrService attrService;
  @Autowired
  private AttrAttrgroupRelationService relationService;

  @PostMapping("/attr/relation")
  public R addRelation(@RequestBody List<AttrGroupRelationVo> vos) {
    relationService.saveBatch(vos);
    return R.ok();
  }

  @GetMapping("/{catelogId}/withattr")
  public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId) {
    // 查出当前分类下的所有属性分组
    List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
    // 出处每个属性分组的所有属性

    return R.ok().put("data", vos);

  }

  @GetMapping("/{attrgroupId}/attr/relation")
  public R attrRelation(@PathVariable("attrgroupId") Long attrGroupId) {
    List<AttrEntity> entityList = attrService.getRelationAttr(attrGroupId);
    return R.ok().put("data", entityList);
  }


  /**
   * 列表
   */
  @RequestMapping("/list/{catelogId}")
  public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId) {
    PageUtils pageUtils = attrGroupService.queryPage(params, catelogId);
    return R.ok().put("page", pageUtils);
  }


  /**
   * 信息
   */
  @RequestMapping("/info/{attrGroupId}")
  public R info(@PathVariable("attrGroupId") Long attrGroupId) {
    AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
    attrGroup.setCatelogPath(categoryService.findCatelogPath(attrGroup.getCatelogId()));
    return R.ok().put("attrGroup", attrGroup);
  }

  /**
   * 保存
   */
  @RequestMapping("/save")
  public R save(@RequestBody AttrGroupEntity attrGroup) {
    attrGroupService.save(attrGroup);

    return R.ok();
  }

  /**
   * 修改
   */
  @RequestMapping("/update")
  public R update(@RequestBody AttrGroupEntity attrGroup) {
    attrGroupService.updateById(attrGroup);

    return R.ok();
  }

  /**
   * 删除
   */
  @RequestMapping("/delete")
  public R delete(@RequestBody Long[] attrGroupIds) {
    attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

    return R.ok();
  }

  @PostMapping("/attr/relation/delete")
  public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos) {
    attrService.deleteRelation(vos);
    return R.ok();
  }

  @GetMapping("/{attrgroupId}/noattr/relation")
  public R attrNoRelation(@PathVariable("attrgroupId") Long attrGroupId,
                          @RequestParam Map<String, Object> params) {
    PageUtils pageUtils = attrService.getNoRelationAttr(attrGroupId, params);
    return R.ok().put("page", pageUtils);
  }
}
